import { Button, Label, ProgressBar } from 'cc';
import {
    BaseBigLoading,
    ccclass,
    cccomponent,
    ccmodifier,
    requireComponent,
} from '../../../extensions/cc-feather/assets/src';

/**
 * - 作者: DoooReyn
 * - 日期: 2023.11.01
 * - 名称: BootBigLoading
 * - 描述: 启动大场景加载页
 */
@ccclass()
@ccmodifier('BootBigLoading')
@requireComponent(Button)
export class BootBigLoading extends BaseBigLoading {
    @cccomponent(ProgressBar, 'ProgressBar')
    proBar: ProgressBar;

    @cccomponent(Label, 'ProgressBar/Label')
    labPercent: Label;

    /**
     * 同步进度
     * @param percent 进度百分比
     */
    private setPercent(percent: number) {
        this.proBar.progress = percent;
        this.labPercent.string = (percent * 100).toFixed(1) + '%';
    }

    /** 加载开始时调用 */
    public async onLoadStart(): Promise<void> {
        this.setPercent(0);
        await super.onLoadStart();
    }

    /**
     * 加载进度更新时调用
     * @param progress 当前量
     * @param total 总量
     */
    public onLoadProgress(progress: number, total: number): void {
        this.setPercent(progress / total);
    }

    /** 加载完成时调用 */
    public async onLoadEnd(): Promise<void> {
        this.setPercent(1);
        await super.onLoadEnd();
    }
}
